package com.dream.inter.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Weixin_Class;
import com.dream.model.Weixin_Teacher;
import com.dream.model.Weixin_User;
import com.dream.model.Weixin_VJR;
import com.dream.system.service.ISysClass;
import com.dream.system.service.ISysClassPlant;
import com.dream.system.service.ISysLable;
import com.dream.system.service.ISysUser;
import com.dream.system.service.ISys_TestPaper;
import com.dream.system.service.TellValidateService;
import com.dream.system.service.Weixin_ClassService;
import com.qcloud.sign.FileCloudSign;

@SuppressWarnings("all")
@Controller
@RequestMapping({ "/inter/weiXinAction.do" })
public class WeixinAction extends SuperController {
	@Autowired
	private ISysClassPlant sysClassPlantImpl;
	@Autowired
	private ISysUser sysUserImpl;
	@Autowired
	private TellValidateService tellValidateServiceImpl;

	@Autowired
	private ISys_TestPaper sys_TestPaperServiceImpl;

	@Autowired
	private ISysLable sysLableService;

	@Autowired
	private ISysClass sysClassServiceImpl;

	@Autowired
	private Weixin_ClassService weixinActionImpl;

	/**
	 * 增中一个学生
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=addStudent" })
	public void addStudent(Weixin_User model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String userName = request.getParameter("userName");
		String userTell = request.getParameter("userTell");
		if (userName == null || "".equals(userName) || userTell == null || "".equals(userTell)) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		this.weixinActionImpl.addWeixin_User(model);
		response.getWriter().write(JSONObject.fromObject(model).toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 增加一个老师
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=addTeacher" })
	public void addTeacher(Weixin_Teacher model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String schoolName = request.getParameter("schoolName");
		String nj = request.getParameter("nj");
		String bj = request.getParameter("bj");
		String userName = request.getParameter("userName");
		String userTell = request.getParameter("userTell");
		String zhucema = createRandom(7);
		model.setCode(zhucema);
		JSONObject list = this.weixinActionImpl.getCodeBySchool(zhucema);
		if (null != list) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		if (schoolName == null || "".equals(schoolName) || userName == null || "".equals(userName) || userTell == null || "".equals(userTell)) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		this.weixinActionImpl.addWeixin_Teacher(model);
		if (model == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		} else {
			response.getWriter().write(JSONObject.fromObject(model).toString());
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}

	}

	/**
	 * 学生下接接口
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=selectSchool" })
	public void selectSchool(Weixin_User model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String schoolName = request.getParameter("schoolName");
		if (schoolName == null || "".equals(schoolName)) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
		}
		JSONArray list = this.weixinActionImpl.selectSchool(model);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		response.getWriter().write(list.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 按openId 查询学生和老师
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=getByST" })
	public void getByST(Weixin_User model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String openId = model.getOpenId();
		if (openId == null || "".equals(openId)) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
		}
		JSONArray list = this.weixinActionImpl.getStudentByOpenId(model);
		if (list == null || list.size() <= 0) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		response.getWriter().write(list.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 按openId 查询学生和老师
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=getCodeBySchool" })
	public void getCodeBySchool(Weixin_User model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String code = model.getCode();
		if (code == null || "".equals(code)) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
		}
		JSONObject list = this.weixinActionImpl.getCodeBySchool(code);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		response.getWriter().write(list.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 按openId 查询学生和老师
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=getByCode2" })
	public void getByCode2(Weixin_User model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String code = model.getCode2();
		if (code == null || "".equals(code)) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		JSONObject list = this.weixinActionImpl.getByCode2(code);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		response.getWriter().write(list.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 按opendCode2 查询学生和老师
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=updateCode2" })
	public void updateCode2(Weixin_User model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String code = model.getCode2();
		if (code == null || "".equals(code) || null == model.getTable_id() || "".equals(model.getTable_id())) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		Object list = this.weixinActionImpl.updateCode2(model);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		// System.out.println(JSONObject.fromObject(list).toString());
		response.getWriter().write(JSONObject.fromObject(list).toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 按opendCode2 查询学生和老师
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=addVurl" })
	public void addVurl(Weixin_VJR model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String code = model.getV_url();
		if (code == null || "".equals(code) || null == model.getV_user() || "".equals(model.getV_user())) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		Object list = this.weixinActionImpl.addVurl(model);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		response.getWriter().write(JSONObject.fromObject(list).toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 查询个人视频列表
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=getVurl" })
	public void getVurl(Weixin_VJR model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String userCode = model.getV_user();
		if (null == model.getV_user() || "".equals(model.getV_user())) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		JSONArray list = this.weixinActionImpl.getVurl(model);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		System.out.println(list.toString());
		response.getWriter().write(list.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 查询老师教的班级
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=getTeacherClass" })
	public void getTeacherClass(Weixin_User model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String userCode = model.getUserTell();
		if (null == model.getUserTell() || "".equals(model.getUserTell())) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		JSONArray list = this.weixinActionImpl.getTeacherClass(userCode);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
	//	System.out.println(list.toString());
		response.getWriter().write(list.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 发布视频
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=sendVJR" })
	public void sendVJR(Weixin_VJR model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String v_url = model.getV_url();
		String banjtable_id = request.getParameter("bj_table_id");
		if (null == model.getV_url() || "".equals(model.getV_url())) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		JSONArray list = this.weixinActionImpl.addStudentVJR(v_url, model.getV_type(), banjtable_id);
		if (list == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
	//	System.out.println(list.toString());
		response.getWriter().write(list.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 发布视频
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=deleteVJR" })
	public void deleteVJR(Weixin_VJR model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		try {
			if (null == model.getTable_id() || "".equals(model.getTable_id())) {
				response.getWriter().write("-1");
				response.getWriter().flush();
				response.getWriter().close();
				return;
			}
			int list = this.weixinActionImpl.deleteVJR(model);
			if (list == 1) {
				response.getWriter().write("1");
				response.getWriter().flush();
				response.getWriter().close();
				return;
			} 
		} catch (Exception e) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		
	}
	
	
	
	/**
	 * 创建指定数量的随机字符串
	 * 
	 * @param length
	 * @return
	 */
	public static String createRandom(int length) {
		String retStr = "";
		String strTable = "1234567890";
		int len = strTable.length();
		boolean bDone = true;
		do {
			retStr = "";
			int count = 0;
			for (int i = 0; i < length; i++) {
				double dblR = Math.random() * len;
				int intR = (int) Math.floor(dblR);
				char c = strTable.charAt(intR);
				if (('0' <= c) && (c <= '9')) {
					count++;
				}
				retStr += strTable.charAt(intR);
			}
			if (count >= 2) {
				bDone = false;
			}
		} while (bDone);

		return retStr;
	}

}
